#ifndef __UDSF_EX_RF_C__
#define __UDSF_EX_RF_C__

#include "app.h"
/*
*   notice:
*   UDSF 同UDF编程：不可调用cstd-lib,只可调用"sdk_ifs_udk_cfg.h"、"sys_api.h"中列出的API函数。
*   UDSF文件内函数均需使用static修饰符，且UDF中以#include "udsf_xxx.c"形式引用
*/


#if defined(USE_EX_RF)

#include "udsf_spi.c"
#include "udsf_delay.c"

/* EX-RF 写命令*/
static void udsf_ex_rf_command(uint8_t cmd){
	uint8_t cmd_buff[1];
	
	cmd_buff[0]	= cmd | 0x80;//(cmd&0x3f) | 0x80;
	EX_RF_CS_PIN = 0;
	udsf_usci0_spi_rw_without_cs((uint8_t*)&cmd_buff,1,NULL,0);
	EX_RF_CS_PIN = 1;
}

/* EX-RF 读寄存器*/
static uint16_t udsf_ex_rf_reg_read(uint8_t cmd){

	uint8_t cmd_buff[1];
	cmd_buff[0] = cmd | 0x40;//(cmd&0x3f)|0x40;

	uint8_t data[2];

	EX_RF_CS_PIN = 0;
	udsf_usci0_spi_rw_without_cs(cmd_buff,1,data,2);
	EX_RF_CS_PIN = 1;

	return ((data[0]<<8) | data[1]);
}

/* EX-RF 写寄存器*/
static void udsf_ex_rf_reg_write(uint8_t cmd,uint16_t data,uint16_t *p_rdback){


	uint8_t cmd_buff[3];
	cmd_buff[0] = cmd;//(cmd&0x3f);
	cmd_buff[1] = data>>8;
	cmd_buff[2] = data;

	EX_RF_CS_PIN = 0;
	udsf_usci0_spi_rw_without_cs(cmd_buff,3,NULL,0);
	EX_RF_CS_PIN = 1;
	if( p_rdback ){
		*p_rdback = udsf_ex_rf_reg_read(cmd);
	}

}

static void udsf_ex_rf_reg_wr_quick(uint8_t* p_cmd,uint16_t* p_data,uint16_t *p_rd,int cnt){

	for(int i=0;i<cnt;i++ ){
		uint8_t cmd   = p_cmd[i];
		uint16_t data = p_data[i];
		uint16_t* p_rdback = &p_rd[i];
	
		uint8_t xxx,yyy;	
		
		EX_RF_CS_PIN = 0;
		USCI0->TRX = cmd;
		__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
		__NOP();__NOP();
		USCI0->TRX = (uint8_t)(data>>8);
		__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
		__NOP();__NOP();
		USCI0->TRX = (uint8_t)data;
		__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
		__NOP();__NOP();
		cmd|=0x40;
		__NOP();__NOP();__NOP();__NOP();
		EX_RF_CS_PIN = 1;

		EX_RF_CS_PIN = 0;
		USCI0->TRX = cmd;
		__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
		__NOP();__NOP();
		USCI0->TRX = 0xff;
		__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
		__NOP();__NOP();__NOP();__NOP();
		xxx = USCI0->TRX;
		USCI0->TRX = 0xff;
		__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
		__NOP();__NOP();__NOP();__NOP();
		yyy = USCI0->TRX;
		*p_rdback = (xxx<<8) | yyy;
		EX_RF_CS_PIN = 1;
	}
}

static void udsf_ex_rf_wavemem_wr_quick(uint8_t row_addr, uint8_t*p_Col_Addr, uint16_t* p_data,uint16_t *p_rd,int cnt){
	
	for(int i=0;i<cnt;i++ ){
		uint8_t col_addr   = p_Col_Addr[i];
		uint16_t data      = p_data[i];
		uint16_t* p_rdback = &p_rd[i];
	
		uint8_t xxx,yyy;
		
		EX_RF_CS_PIN = 0;
		USCI0->TRX = 0x3E;
		__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
		__NOP();__NOP();
		USCI0->TRX = row_addr;
		__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
		__NOP();__NOP();
		USCI0->TRX = col_addr;
		__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
		__NOP();__NOP();
		USCI0->TRX = (uint8_t)(data>>8);
		__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
		__NOP();__NOP();
		USCI0->TRX = (uint8_t)data;
		__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
		__NOP();__NOP();
		
		__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
		EX_RF_CS_PIN = 1;

		EX_RF_CS_PIN = 0;
		USCI0->TRX = 0x7e;
		__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
		__NOP();__NOP();
		USCI0->TRX = row_addr;
		__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
		__NOP();__NOP();
		USCI0->TRX = col_addr;
		__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
		__NOP();__NOP();
		USCI0->TRX = 0xff;
		__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
		__NOP();__NOP();__NOP();__NOP();
		xxx = USCI0->TRX;
		USCI0->TRX = 0xff;
		__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
		__NOP();__NOP();__NOP();__NOP();
		yyy = USCI0->TRX;
		*p_rdback = (xxx<<8) | yyy;
		EX_RF_CS_PIN = 1;
	}
}

/* EX-RF 读wavemem*/
static uint16_t udsf_ex_rf_wavemem_read(uint8_t Row_Addr, uint8_t Col_Addr)
{
	uint8_t cmd_buff[3];
	cmd_buff[0] = 0x7E;
	cmd_buff[1] = Row_Addr;// & 0x7F;
	cmd_buff[2] = Col_Addr;//(Col_Addr << 5) & 0xE0;

	uint8_t data[2];
	EX_RF_CS_PIN = 0;
	udsf_usci0_spi_rw_without_cs(cmd_buff,3,data,2);
	EX_RF_CS_PIN = 1;

	return ((data[0]<<8) | data[1]);
}
/* EX-RF 写wavemem*/
static void udsf_ex_rf_wavemem_write(uint8_t Row_Addr, uint8_t Col_Addr, uint16_t u16Data,uint16_t* p_rdback)
{
	uint8_t cmd_buff[5];
	cmd_buff[0] = 0x3E;
	cmd_buff[1] = Row_Addr;// & 0x7F;
	cmd_buff[2] = Col_Addr;//(Col_Addr << 5) & 0xE0;
	cmd_buff[3] = (u16Data >> 8);// & 0xFF;
	cmd_buff[4] = u16Data;// & 0xFF;

	EX_RF_CS_PIN = 0;
	udsf_usci0_spi_rw_without_cs(cmd_buff,5,NULL,0);
	EX_RF_CS_PIN = 1;

	if( p_rdback ){
		*p_rdback = udsf_ex_rf_wavemem_read(Row_Addr,Col_Addr);
	}
}

static void udsf_ex_rf_spi_init(uint8_t clk_div){

	udsf_usci0_spi_init_without_cs(EX_RF_CS_PIN_ID,clk_div,1,0);

}

static void udsf_ex_rf_spi_deinit(void){
	udsf_usci0_spi_deinit_without_cs();
}

static int udsf_ex_rf_chip_ctrl(uint8_t enable){
	int res = 0;

	if( enable ){
		EX_RF_CS_PIN = 1;
		EX_RF_CHIPEN_PIN = 0;
		GPIO->PMODE |= ((1<<EX_RF_CS_PIN_ID) | (1<<EX_RF_CHIPEN_PIN_ID));
		EX_RF_CHIPEN_PIN = 1;

		#if 0
		/*just for test: output clk */
		uint16_t read_back;
		udsf_ex_rf_reg_write(0x0e,0x0001,&read_back);
		//res = read_back;
		if( read_back != 0x0001 ){
		 	res = -1;
		}
		
		#endif
	}else{
		EX_RF_CS_PIN = 1;
		EX_RF_CHIPEN_PIN = 0;
		GPIO->PMODE &= ~((1<<EX_RF_CS_PIN_ID) | (1<<EX_RF_CHIPEN_PIN_ID));
	}
		
	return res;
}
#endif

#endif
